/**********************************************************************
gc_12_structural_counterfactuals.do

**********************************************************************/
**********
* SET UP *
**********
clear all
set matsize 2000
set more 1

* location for dofiles *
cd "T:\_Projet_4915\dofiles"
global dir "`c(pwd)'"
cd $dir

set seed 8397740

sysdir set PERSONAL "T:\_Projet_4915\ado"

* specify file locations *
global project_folder "\_Projet_4915"
global data_folder "\_Projet_4915\DATA"
global output_folder "\_Projet_4915\ResultsFolder"
global temp "temp"

local datadir T:\${data_folder}\

*********
* GATES *
*********
* Specify which data you want to work with (synthetic = syn, real = rl) *
local ext = "rl"

* Specify which gender to process (men = men) *
local gender = "men"

* Specify first observed age (24 25 26) *
local a = 25

* Specify occupational definition to use (occ_version 1 2)
local v = 2

* Specify firm class definition (1 2 3 4 5 6)
local d = 6

* Specify the number of unobservable types *
global types  "5"

* Specify earnings data generating process *
local y_dgp = 3

* Counterfactual 5: 50% subsidy to enter unincorporated or incorporated entrepreneurship at age 30 (available to non-employed people) *
local gate5 = 1

* Counterfactual 6: 50% subsidy to enter incorporated entrepreneurship at age 30 (available for everyone) *
local gate6 = 1

* STEP X: delete all intermediate datasets *
local gateX = 1

*******************************************
* MAKE A GLOBAL VARIABLE FOR TODAY'S DATE *
*******************************************
local tyr = substr("$S_DATE",8,4)
local tmo = substr("$S_DATE",4,3)
local tmd = trim(substr("$S_DATE",1,2))

* make day of month two digits *
local wl = length("`tmd'")
if `wl'==1 {
local tmd2 ="0"+"`tmd'"
}
if `wl'!=1 {
local tmd2 ="`tmd'"
}

* get numeric month, make it two digits *
local di="`tmd2'"+"`tmo'"+"`tyr'"
local edate = date("`di'", "DMY")
local mono = month(`edate')
local ml = length("`mono'")
if `ml'==1 {
local mono2 ="0"+"`mono'"
}
if `ml'!=1 {
local mono2 ="`mono'"
}

* put final date together *
global date = "`tyr'"+"`mono2'"+"`tmd2'"

****************************************
* DEFINE LITTLE PROGRAMS TO PRINT TIME *
****************************************
program define starttime
	display "Started processing at $S_TIME on $S_DATE"
end

program define endtime
	display "Finished processing at $S_TIME on $S_DATE"
end


************************
* START OF THE PROGRAM *
************************
foreach n_types of global types ///
{
* start log file *
quietly capture log close
quietly log using gc_12_structural_counterfactuals_p2_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'_`ext', text replace

********************
* Counterfactual 5 *
********************
disp "***** Started processing STEP 5 *****"
starttime
if 1 == `gate5' ///
{
	disp "***** Counterfactual 5 *****"
	disp "***** Time is now $S_TIME *****"
	
	local datadir T:\${data_folder}\
	use "`datadir'gc_structural_fit_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", clear
	keep if age == 1
	keep pid_int state
	compress
	save "`datadir'gc_TEMP_C5.dta", replace
	use "`datadir'gc_analyze_me_tmp_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", clear
	keep if age == 1
	* keep a 20% random sample of individuals *
	gen draw = runiform()
	replace draw = . if state != 1
	bys pid_int: egen draw2 = max(draw)
	keep if draw2 <= 1
	drop draw*
	rename state state_index
	merge m:1 pid_int state_index using "`datadir'gc_structural_Qs_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta"
	drop if _merge == 2
	drop _merge
	rename state_index state
	reshape wide q, i(pid_int) j(state)
	keep pid_int q* age Laction exper_11 exper_12 exper_13 exper_20 exper_30 busiden_entry_cat
	order pid_int q* age Laction exper_11 exper_12 exper_13 exper_20 exper_30 busiden_entry_cat
	
	merge 1:1 pid_int using "`datadir'gc_TEMP_C5.dta"
	keep if _merge == 3
	drop _merge
	
	global keep_vars ""
	foreach var in ///
	"Laction" "age" /// 
	"exper_11" "exper_12" "exper_13" ///
	"exper_20" "exper_30" ///
	{
		gen `var'_index = `var'
		global keep_vars "$keep_vars `var'_index"
	}
	gen Laction_rl = Laction
	global keep_vars "pid_int state q* $keep_vars Laction_rl busiden_entry_cat"
	keep $keep_vars
	order $keep_vars
	
	local big_T = 1
	while `big_T' <= 10 ///
	{
	gen V_0 = 1
	
	***************************
	* STEP 1.2: FIX ACTION j' *
	***************************
	* Create synthetic variables *
	foreach var in ///
	"Laction" "age" /// 
	"exper_11" "exper_12" "exper_13" ///
	"exper_20" "exper_30" ///
	{
		gen `var' = `var'_index
	}
	
	* construct synthetic aggregate experience variables *
	gen exper_10 = exper_11 + exper_12 + exper_13	
	gen age_sq = age*age
	
	* construct polynomials of experience *
	foreach var in ///
	"exper_10" "exper_20" "exper_30" ///
	{
		gen `var'_sq = `var'*`var'
	}
	
	* Bring CCP matrix using appropriate STER files from first stage *
	
	* Create CCPs *
	foreach act in ///
	"11" "12" "13" ///
	"20" "30" "90" ///
	{
		gen CCP`act' = .
	}		
	
	local counter = 1
	
	* Loop through state *
	forvalues s = 1(1)`n_types' ///
	{
	* Loop through Laction *
	foreach Lact in ///
	"11" "12" "13" ///
	"20" "30" "90" ///
	{
		* Loop through action dummies *
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			disp "***** Now running linear probability model locally for: CCP action = `act', Laction = `Lact', and type = `s' *****"
			disp "***** Iteration `counter', time is now $S_TIME *****"
			local datadir T:\${data_folder}\gc_ster\
			estimates use "`datadir'gc_structural_CCP`act'_L`Lact'_s`s'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
			predict tmpCCP`act'_`Lact'_`s' if Laction == `Lact' & state == `s'
			estimates clear
			replace CCP`act' = tmpCCP`act'_`Lact'_`s' if Laction == `Lact' & state == `s'
			drop tmp*
			local counter = `counter' + 1
		}
	}
	}

	foreach act in ///
	"11" "12" "13" ///
	"20" "30" "90" ///
	{
		replace CCP`act' = 0.0000001 if CCP`act' <= 0.0000001
		replace CCP`act' = 0.9999999 if CCP`act' >= 0.9999999 & CCP`act' !=.
	}
	
	gen CCP_TOT = CCP11 + CCP12 + CCP13 + CCP20 + CCP30 + CCP90
	foreach act in ///
	"11" "12" "13" ///
	"20" "30" "90" ///
	{
		replace CCP`act' = CCP`act'/CCP_TOT
	}
	drop CCP_TOT
	
	* Pile up the CCPs into a sort of cdf *
	gen a = CCP11
	gen b = a + CCP12
	gen c = b + CCP13
	gen d = c + CCP20
	gen e = d + CCP30
	gen f = e + CCP90
	
	* Draw uniform (0,1) *
	gen draw = runiform()
	
	* Draw action *
	gen zaction_jp = 0
	replace zaction_jp = 11 if draw < a
	replace zaction_jp = 12 if draw >= a & draw < b
	replace zaction_jp = 13 if draw >= b & draw < c
	replace zaction_jp = 20 if draw >= c & draw < d
	replace zaction_jp = 30 if draw >= d & draw < e
	replace zaction_jp = 90 if draw >= e & draw <= 1
	
	drop CCP*
	
	* Drop all synthetic variables *
	keep $keep_vars z* V_*
	
	****************************
	* STEP 1.3: FIX ACTION j'' *
	****************************
	* Create synthetic variables *
	foreach var in ///
	"Laction" "age" /// 
	"exper_11" "exper_12" "exper_13" ///
	"exper_20" "exper_30" ///
	{
		gen `var' = `var'_index
	}
	
	* Forward simulate synthetic variables *
	replace Laction = zaction_jp
	replace age = age + 1

	foreach act in ///
	"11" "12" "13" ///
	"20" "30" ///
	{
		replace exper_`act' = exper_`act' + 1 if zaction_jp == `act'
	}
	
	* construct synthetic aggregate experience variables *
	gen exper_10 = exper_11 + exper_12 + exper_13	
	gen age_sq = age*age
	
	* construct polynomials of experience *
	foreach var in ///
	"exper_10" "exper_20" "exper_30" ///
	{
		gen `var'_sq = `var'*`var'
	}
	
	* Bring CCP matrix using appropriate STER files from first stage *
	
	* Create CCPs *
	foreach act in ///
	"11" "12" "13" ///
	"20" "30" "90" ///
	{
		gen CCP`act' = .
	}
	
	local counter = 1
	* Loop through state *
	forvalues s = 1(1)`n_types' ///
	{
	* Loop through Laction *
	foreach Lact in ///
	"11" "12" "13" ///
	"20" "30" "90" ///
	{
		* Loop through action dummies *
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			disp "***** Now running linear probability model locally for: CCP action = `act', Laction = `Lact', and type = `s' *****"
			disp "***** Iteration `counter', time is now $S_TIME *****"
			local datadir T:\${data_folder}\gc_ster\
			estimates use "`datadir'gc_structural_CCP`act'_L`Lact'_s`s'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
			predict tmpCCP`act'_`Lact'_`s' if Laction == `Lact' & state == `s'
			estimates clear
			replace CCP`act' = tmpCCP`act'_`Lact'_`s' if Laction == `Lact' & state == `s'
			drop tmp*
			local counter = `counter' + 1
		}
	}
	}
	
	foreach act in ///
	"11" "12" "13" ///
	"20" "30" "90" ///
	{
		replace CCP`act' = 0.0000001 if CCP`act' <= 0.0000001
		replace CCP`act' = 0.9999999 if CCP`act' >= 0.9999999 & CCP`act' !=.
	}
	
	gen CCP_TOT = CCP11 + CCP12 + CCP13 + CCP20 + CCP30 + CCP90
	foreach act in ///
	"11" "12" "13" ///
	"20" "30" "90" ///
	{
		replace CCP`act' = CCP`act'/CCP_TOT
	}
	drop CCP_TOT
	
	* Pile up the CCPs into a sort of cdf *
	gen a = CCP11
	gen b = a + CCP12
	gen c = b + CCP13
	gen d = c + CCP20
	gen e = d + CCP30
	gen f = e + CCP90
	
	* Draw uniform (0,1) *
	gen draw = runiform()
	
	* Draw action j'' *
	gen zaction_jpp = 0
	replace zaction_jpp = 11 if draw < a
	replace zaction_jpp = 12 if draw >= a & draw < b
	replace zaction_jpp = 13 if draw >= b & draw < c
	replace zaction_jpp = 20 if draw >= c & draw < d
	replace zaction_jpp = 30 if draw >= d & draw < e
	replace zaction_jpp = 90 if draw >= e & draw <= 1
	drop CCP*
	
	* Drop all synthetic variables *
	keep $keep_vars z* V_*
	
	***************************************************
	* STEP 1.4: FOR EACH j in J CALCULATE OFFSET TERM *
	***************************************************
	foreach j in ///
	"11" "12" "13" ///
	"20" "30" "90" ///
	{
		* Pick action j *
		gen zaction_j = `j'
		* Pick action cj *
		gen zaction_cj = zaction_jp
		* Pick action cjp *
		gen zaction_cjp = zaction_j
		* Pick action cjpp *
		gen zaction_cjpp = zaction_jpp
		
		****************************
		* CCPs and income associated with j *
		****************************
		* Create synthetic variables *
		foreach var in ///
		"Laction" "age" /// 
		"exper_11" "exper_12" "exper_13" ///
		"exper_20" "exper_30" ///
		{
			gen `var' = `var'_index
		}
		
		* construct synthetic aggregate experience variables *
		gen exper_10 = exper_11 + exper_12 + exper_13	
		gen age_sq = age*age
		
		* construct polynomials of experience *
		foreach var in ///
		"exper_10" "exper_20" "exper_30" ///
		{
			gen `var'_sq = `var'*`var'
		}
	
		* Bring ln_y_hat matrix using appropriate STER files from first stage *
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" ///
		{
			disp "***** Now estimating parameters of the wage equation for action: `act' *****"
			disp "***** Time is now $S_TIME *****"
			local datadir T:\${data_folder}\gc_ster\
			estimates use "`datadir'gc_structural_y`act'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
			predict ln_y_hat_`act'
			estimates clear
		}
		
		* Pick the relevant ln_y_hats *
		gen zln_y_hat_j = 0
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" ///
		{
			replace zln_y_hat_j = ln_y_hat_`act' if zaction_j == `act'
		}
		*** COUNTERFACTUAL CHANGE ***
		if `big_T' == 5 ///
		{
			disp "BIG T is 1. COUNTERFACTUAL ACTIVATED"
			replace zln_y_hat_j = zln_y_hat_j + ln(2) if zaction_j == 20 & Laction == 90
			replace zln_y_hat_j = zln_y_hat_j + ln(2) if zaction_j == 30 & Laction == 90
		}
		replace zln_y_hat_j = ln(10400) if zaction_j == 90
		drop ln_y_hat*
	
		* Drop all synthetic variables *
		keep $keep_vars z* V_*
		
		****************************
		* CCPs and income associated with j'. *
		****************************
		* Create synthetic variables *
		foreach var in ///
		"Laction" "age" /// 
		"exper_11" "exper_12" "exper_13" ///
		"exper_20" "exper_30" ///
		{
			gen `var' = `var'_index
		}
		
		* Forward simulate synthetic variables *
		replace Laction = zaction_j
		replace age = age + 1
	
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" ///
		{
			replace exper_`act' = exper_`act' + 1 if zaction_j == `act'
		}
		
		* construct synthetic aggregate experience variables *
		gen exper_10 = exper_11 + exper_12 + exper_13	
		gen age_sq = age*age
		
		* construct polynomials of experience *
		foreach var in ///
		"exper_10" "exper_20" "exper_30" ///
		{
			gen `var'_sq = `var'*`var'
		}
		
		* Bring CCP matrix using appropriate STER files from first stage *
		
		* Create CCPs *
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			gen CCP`act' = .
		}
		
		local counter = 1
		* Loop through state *
		forvalues s = 1(1)`n_types' ///
		{
		* Loop through Laction *
		foreach Lact in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			* Loop through action dummies *
			foreach act in ///
			"11" "12" "13" ///
			"20" "30" "90" ///
			{
				disp "***** Now running linear probability model locally for: CCP action = `act', Laction = `Lact', and type = `s' *****"
				disp "***** Iteration `counter', time is now $S_TIME *****"
				local datadir T:\${data_folder}\gc_ster\
				estimates use "`datadir'gc_structural_CCP`act'_L`Lact'_s`s'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
				predict tmpCCP`act'_`Lact'_`s' if Laction == `Lact' & state == `s'
				estimates clear
				replace CCP`act' = tmpCCP`act'_`Lact'_`s' if Laction == `Lact' & state == `s'
				drop tmp*
				local counter = `counter' + 1
			}
		}
		}
		
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			replace CCP`act' = 0.0000001 if CCP`act' <= 0.0000001
			replace CCP`act' = 0.9999999 if CCP`act' >= 0.9999999 & CCP`act' !=.
		}
		
		gen CCP_TOT = CCP11 + CCP12 + CCP13 + CCP20 + CCP30 + CCP90
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			replace CCP`act' = CCP`act'/CCP_TOT
		}
		
		drop CCP_TOT
		
		* Pick the relevant CCPs *
		gen zCCP_jp = .
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			replace zCCP_jp = CCP`act' if zaction_jp == `act'
		}
		
		drop CCP*
		
		* Bring ln_y_hat matrix using appropriate STER files from first stage *
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" ///
		{
			disp "***** Now estimating parameters of the wage equation for action: `act' *****"
			disp "***** Time is now $S_TIME *****"
			local datadir T:\${data_folder}\gc_ster\
			estimates use "`datadir'gc_structural_y`act'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
			predict ln_y_hat_`act'
			estimates clear
		}
		
		* Pick the relevant ln_y_hats *
		gen zln_y_hat_jp = 0
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" ///
		{
			replace zln_y_hat_jp = ln_y_hat_`act' if zaction_jp == `act'
		}
		replace zln_y_hat_jp = ln(10400) if zaction_jp == 90
		drop ln_y_hat*
		
		keep $keep_vars z* V_*
		
		****************************************
		* CCPs and income associated with j''. *
		****************************************
		* Create synthetic variables *
		foreach var in ///
		"Laction" "age" /// 
		"exper_11" "exper_12" "exper_13" ///
		"exper_20" "exper_30" ///
		{
			gen `var' = `var'_index
		}
		
		* Forward simulate synthetic variables *
		replace Laction = zaction_jp
		replace age = age + 2
	
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" ///
		{
			replace exper_`act' = exper_`act' + 1 if zaction_j == `act'
			replace exper_`act' = exper_`act' + 1 if zaction_jp == `act'
		}
		
		* construct synthetic aggregate experience variables *
		gen exper_10 = exper_11 + exper_12 + exper_13	
		gen age_sq = age*age
		
		* construct polynomials of experience *
		foreach var in ///
		"exper_10" "exper_20" "exper_30" ///
		{
			gen `var'_sq = `var'*`var'
		}
		
		* Bring CCP matrix using appropriate STER files from first stage *
		
		* Create CCPs *
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			gen CCP`act' = .
		}
		
		local counter = 1
		* Loop through state *
		forvalues s = 1(1)`n_types' ///
		{
		* Loop through Laction *
		foreach Lact in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			* Loop through action dummies *
			foreach act in ///
			"11" "12" "13" ///
			"20" "30" "90" ///
			{
				disp "***** Now running linear probability model locally for: CCP action = `act', Laction = `Lact', and type = `s' *****"
				disp "***** Iteration `counter', time is now $S_TIME *****"
				local datadir T:\${data_folder}\gc_ster\
				estimates use "`datadir'gc_structural_CCP`act'_L`Lact'_s`s'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
				predict tmpCCP`act'_`Lact'_`s' if Laction == `Lact' & state == `s'
				estimates clear
				replace CCP`act' = tmpCCP`act'_`Lact'_`s' if Laction == `Lact' & state == `s'
				drop tmp*
				local counter = `counter' + 1
			}
		}
		}
		
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			replace CCP`act' = 0.0000001 if CCP`act' <= 0.0000001
			replace CCP`act' = 0.9999999 if CCP`act' >= 0.9999999 & CCP`act' !=.
		}
		
		gen CCP_TOT = CCP11 + CCP12 + CCP13 + CCP20 + CCP30 + CCP90
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			replace CCP`act' = CCP`act'/CCP_TOT
		}
		drop CCP_TOT
		
		* Pick the relevant CCPs *
		gen zCCP_jpp = .
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			replace zCCP_jpp = CCP`act' if zaction_jpp == `act'
		}
	
		drop CCP*
		
		* Bring ln_y_hat matrix using appropriate STER files from first stage *
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" ///
		{
			disp "***** Now estimating parameters of the wage equation for action: `act' *****"
			disp "***** Time is now $S_TIME *****"
			local datadir T:\${data_folder}\gc_ster\
			estimates use "`datadir'gc_structural_y`act'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
			predict ln_y_hat_`act'
			estimates clear
		}
		
		* Pick the relevant ln_y_hats *
		gen zln_y_hat_jpp = 0
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" ///
		{
			replace zln_y_hat_jpp = ln_y_hat_`act' if zaction_jpp == `act'
		}
		replace zln_y_hat_jpp = ln(10400) if zaction_jpp == 90
		drop ln_y_hat*
		
		* Drop all synthetic variables *
		keep $keep_vars z* V_*
	
		***************************************
		* CCPs and income associated with cj. *
		***************************************
		* Create synthetic variables *
		foreach var in ///
		"Laction" "age" /// 
		"exper_11" "exper_12" "exper_13" ///
		"exper_20" "exper_30" ///
		{
			gen `var' = `var'_index
		}
		
		* construct synthetic aggregate experience variables *
		gen exper_10 = exper_11 + exper_12 + exper_13	
		gen age_sq = age*age
		
		* construct polynomials of experience *
		foreach var in ///
		"exper_10" "exper_20" "exper_30" ///
		{
			gen `var'_sq = `var'*`var'
		}
		
		* Bring ln_y_hat matrix using appropriate STER files from first stage *
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" ///
		{
			disp "***** Now estimating parameters of the wage equation for action: `act' *****"
			disp "***** Time is now $S_TIME *****"
			local datadir T:\${data_folder}\gc_ster\
			estimates use "`datadir'gc_structural_y`act'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
			predict ln_y_hat_`act'
			estimates clear
		}
		
		* Pick the relevant ln_y_hats *
		gen zln_y_hat_cj = 0
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" ///
		{
			replace zln_y_hat_cj = ln_y_hat_`act' if zaction_cj == `act'
		}
		*** COUNTERFACTUAL CHANGE ***
		if `big_T' == 5 ///
		{
			disp "BIG T is 1. COUNTERFACTUAL ACTIVATED"
			replace zln_y_hat_cj = zln_y_hat_cj + ln(2) if zaction_cj == 20 & Laction == 90
			replace zln_y_hat_cj = zln_y_hat_cj + ln(2) if zaction_cj == 30 & Laction == 90
		}
		replace zln_y_hat_cj = ln(10400) if zaction_cj == 90
		drop ln_y_hat*
		
		* Drop all synthetic variables *
		keep $keep_vars z* V_*
		
		****************************************
		* CCPs and income associated with cj'. *
		****************************************
		* Create synthetic variables *
		foreach var in ///
		"Laction" "age" /// 
		"exper_11" "exper_12" "exper_13" ///
		"exper_20" "exper_30" ///
		{
			gen `var' = `var'_index
		}
		
		* Forward simulate synthetic variables *
		replace Laction = zaction_cj
		replace age = age + 1
		
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" ///
		{
			replace exper_`act' = exper_`act' + 1 if zaction_cj == `act'
		}
		
		* construct synthetic aggregate experience variables *
		gen exper_10 = exper_11 + exper_12 + exper_13	
		gen age_sq = age*age
		
		* construct polynomials of experience *
		foreach var in ///
		"exper_10" "exper_20" "exper_30" ///
		{
			gen `var'_sq = `var'*`var'
		}
		
		* Bring CCP matrix using appropriate STER files from first stage *
		
		* Create CCPs *
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			gen CCP`act' = .
		}		
		
		local counter = 1
		
		* Loop through state *
		forvalues s = 1(1)`n_types' ///
		{
		* Loop through Laction *
		foreach Lact in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			* Loop through action dummies *
			foreach act in ///
			"11" "12" "13" ///
			"20" "30" "90" ///
			{
				disp "***** Now running linear probability model locally for: CCP action = `act', Laction = `Lact', and type = `s' *****"
				disp "***** Iteration `counter', time is now $S_TIME *****"
				local datadir T:\${data_folder}\gc_ster\
				estimates use "`datadir'gc_structural_CCP`act'_L`Lact'_s`s'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
				predict tmpCCP`act'_`Lact'_`s' if Laction == `Lact' & state == `s'
				estimates clear
				replace CCP`act' = tmpCCP`act'_`Lact'_`s' if Laction == `Lact' & state == `s'
				drop tmp*
				local counter = `counter' + 1
			}
		}
		}
	
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			replace CCP`act' = 0.0000001 if CCP`act' <= 0.0000001
			replace CCP`act' = 0.9999999 if CCP`act' >= 0.9999999 & CCP`act' !=.
		}
		
		gen CCP_TOT = CCP11 + CCP12 + CCP13 + CCP20 + CCP30 + CCP90
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			replace CCP`act' = CCP`act'/CCP_TOT
		}
		drop CCP_TOT
		
		* Pick the relevant CCPs *
		gen zCCP_cjp = .
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			replace zCCP_cjp = CCP`act' if zaction_cjp == `act'
		}
		
		drop CCP*
		
		* Bring ln_y_hat matrix using appropriate STER files from first stage *
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" ///
		{
			disp "***** Now estimating parameters of the wage equation for action: `act' *****"
			disp "***** Time is now $S_TIME *****"
			local datadir T:\${data_folder}\gc_ster\
			estimates use "`datadir'gc_structural_y`act'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
			predict ln_y_hat_`act'
			estimates clear
		}
		
		* Pick the relevant ln_y_hats *
		gen zln_y_hat_cjp = 0
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" ///
		{
			replace zln_y_hat_cjp = ln_y_hat_`act' if zaction_cjp == `act'
		}
		replace zln_y_hat_cjp = ln(10400) if zaction_cjp == 90
		drop ln_y_hat*
		
		* Drop all synthetic variables *
		keep $keep_vars z* V_*
		
		******************************
		* CCPs associated with cj''. *
		******************************
		* Create synthetic variables *
		foreach var in ///
		"Laction" "age" /// 
		"exper_11" "exper_12" "exper_13" ///
		"exper_20" "exper_30" ///
		{
			gen `var' = `var'_index
		}
		
		* Forward simulate synthetic variables *
		replace Laction = zaction_cjp
		replace age = age + 2
	
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" ///
		{
			replace exper_`act' = exper_`act' + 1 if zaction_cj == `act'
			replace exper_`act' = exper_`act' + 1 if zaction_cjp == `act'
		}
		
		* construct synthetic aggregate experience variables *
		gen exper_10 = exper_11 + exper_12 + exper_13	
		gen age_sq = age*age
		
		* construct polynomials of experience *
		foreach var in ///
		"exper_10" "exper_20" "exper_30" ///
		{
			gen `var'_sq = `var'*`var'
		}
		
		* Bring CCP matrix using appropriate STER files from first stage *
		
		* Create CCPs *
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			gen CCP`act' = .
		}
		
		local counter = 1
		* Loop through state *
		forvalues s = 1(1)`n_types' ///
		{
		* Loop through Laction *
		foreach Lact in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			* Loop through action dummies *
			foreach act in ///
			"11" "12" "13" ///
			"20" "30" "90" ///
			{
				disp "***** Now running linear probability model locally for: CCP action = `act', Laction = `Lact', and type = `s' *****"
				disp "***** Iteration `counter', time is now $S_TIME *****"
				local datadir T:\${data_folder}\gc_ster\
				estimates use "`datadir'gc_structural_CCP`act'_L`Lact'_s`s'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
				predict tmpCCP`act'_`Lact'_`s' if Laction == `Lact' & state == `s'
				estimates clear
				replace CCP`act' = tmpCCP`act'_`Lact'_`s' if Laction == `Lact' & state == `s'
				drop tmp*
				local counter = `counter' + 1
			}
		}
		}
		
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			replace CCP`act' = 0.0000001 if CCP`act' <= 0.0000001
			replace CCP`act' = 0.9999999 if CCP`act' >= 0.9999999 & CCP`act' !=.
		}
		
		gen CCP_TOT = CCP11 + CCP12 + CCP13 + CCP20 + CCP30 + CCP90
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			replace CCP`act' = CCP`act'/CCP_TOT
		}
		drop CCP_TOT
		
		* Pick the relevant CCPs *
		gen zCCP_cjpp = .
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			replace zCCP_cjpp = CCP`act' if zaction_cjpp == `act'
		}
	
		drop CCP*
		
		* Bring ln_y_hat matrix using appropriate STER files from first stage *
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" ///
		{
			disp "***** Now estimating parameters of the wage equation for action: `act' *****"
			disp "***** Time is now $S_TIME *****"
			local datadir T:\${data_folder}\gc_ster\
			estimates use "`datadir'gc_structural_y`act'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
			predict ln_y_hat_`act'
			estimates clear
		}
		
		* Pick the relevant ln_y_hats *
		gen zln_y_hat_cjpp = 0
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" ///
		{
			replace zln_y_hat_cjpp = ln_y_hat_`act' if zaction_cjpp == `act'
		}
		replace zln_y_hat_cjpp = ln(10400) if zaction_cjpp == 90
		drop ln_y_hat*
		
		* Drop all synthetic variables *
		keep $keep_vars z* V_*
		
		local beta = .90
		display "`beta'"
		
		*************************
		* Calculate offset term *
		*************************
		gen Offset = `beta'*log(zCCP_jp/zCCP_cjp) + `beta'*`beta'*log(zCCP_jpp/zCCP_cjpp)
		
		********************************************************************
		* Calculate Delta U using estimated parameters of utility function *
		********************************************************************
		gen z_lny = zln_y_hat_j-zln_y_hat_cj + `beta'*(zln_y_hat_jp-zln_y_hat_cjp) + `beta'*`beta'*(zln_y_hat_jpp-zln_y_hat_cjpp)
	
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" ///
		{
			gen amen`act'_j = 0
			replace amen`act'_j = 1-`beta' if zaction_j == `act'
			gen amen`act'_jp = 0
			replace amen`act'_jp = -1*(1-`beta') if zaction_jp == `act'
			gen zamen`act' = amen`act'_j + amen`act'_jp
			drop amen*
		}
		
		display "`beta'"
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" ///
		{
			foreach Lact in ///
			"11" "12" "13" ///
			"20" "30" "90" ///
			{
				gen cost_act`Lact'_`act'_z_j = 0
				replace cost_act`Lact'_`act'_z_j = 1 if Laction_index == `Lact' & zaction_j == `act' & Laction_index != zaction_j
				gen cost_act`Lact'_`act'_z_jp = 0
				replace cost_act`Lact'_`act'_z_jp = -1 if Laction_index == `Lact' & zaction_jp == `act' & Laction_index != zaction_jp
				gen cost_act`Lact'_`act'_j_jp = 0
				replace cost_act`Lact'_`act'_j_jp = `beta' if zaction_j == `Lact' & zaction_jp == `act' & zaction_j != zaction_jp
				gen cost_act`Lact'_`act'_jp_j = 0
				replace cost_act`Lact'_`act'_jp_j = -`beta' if zaction_jp == `Lact' & zaction_j == `act' & zaction_jp != zaction_j
				gen cost_act`Lact'_`act'_jp_jpp = 0
				replace cost_act`Lact'_`act'_jp_jpp = `beta'*`beta' if zaction_jp == `Lact' & zaction_jpp == `act' & zaction_jp != zaction_jpp
				gen cost_act`Lact'_`act'_j_jpp = 0
				replace cost_act`Lact'_`act'_j_jpp = -`beta'*`beta' if zaction_j == `Lact' & zaction_jpp == `act' & zaction_j != zaction_jpp
				gen zcost_act`Lact'_`act' = cost_act`Lact'_`act'_z_j+cost_act`Lact'_`act'_z_jp+cost_act`Lact'_`act'_j_jp+cost_act`Lact'_`act'_jp_j+cost_act`Lact'_`act'_jp_jpp + cost_act`Lact'_`act'_j_jpp
				drop cost*
			}
		}

		disp "***** Time is now $S_TIME *****"
		local datadir T:\${data_folder}\gc_ster\
		estimates use "`datadir'gc_structural_util_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
		predict Delta_U
		estimates clear
	
		* Now calculate v_j *
		gen v_`j' = Delta_U - Offset
	
		* Now calculate e_j *
		gen rgumbel_`j' = -log(-log(runiform()))
			
		* Now calculate V_j *
		gen V_`j' = v_`j' + rgumbel_`j'

		keep $keep_vars zaction_jp zaction_jpp V_*
	}
		
	**********************************************************
	* Now choose action that maximizes V_j, call that action *
	**********************************************************
	gen maxV = max(V_11,V_12,V_13,V_20,V_30,V_90)
	gen action = 0
	foreach act in ///
	"11" "12" "13"  ///
	"20" "30" "90" ///
	{
		replace action = `act' if round(V_`act',.001) == round(maxV,.001)
	}

	* save cross-section before updating state variables *
	preserve
	foreach var in ///
	"Laction" "age" /// 
	"exper_11" "exper_12" "exper_13" ///
	"exper_20" "exper_30" ///
	{
		gen `var' = `var'_index
	}
	keep pid_int state q* age action Laction exper_11 exper_12 exper_13 exper_20 exper_30
	order pid_int state q* age action Laction exper_11 exper_12 exper_13 exper_20 exper_30
	gen t = `big_T'
	* construct aggregate experience variables *
	gen exper_10 = exper_11 + exper_12 + exper_13	
	gen age_sq = age*age
	
	* construct polynomials of experience *
	foreach var in ///
	"exper_10" "exper_20" "exper_30" ///
	{
		gen `var'_sq = `var'*`var'
	}
	
	* Simulate log earnings using estimated payoff functions *
	* Bring ln_y_hat matrix using appropriate STER files from first stage *
	foreach act in ///
	"11" "12" "13" ///
	"20" "30" ///
	{
		disp "***** Now estimating parameters of the wage equation for action: `act' *****"
		disp "***** Time is now $S_TIME *****"
		local datadir T:\${data_folder}\gc_ster\
		estimates use "`datadir'gc_structural_y`act'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
		predict ln_y_hat_`act'
		estimates clear
	}
		
	* Pick the relevant ln_y_hat *
	gen ln_y_hat = .
	foreach act in ///
	"11" "12" "13" ///
	"20" "30" ///
	{
		replace ln_y_hat = ln_y_hat_`act' if action == `act'
	}
	drop ln_y_hat_*
	
	local datadir T:\${data_folder}\
	disp "`datadir'"
	compress
	save "`datadir'gc_tmpfitc5_`big_T'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", replace
	restore
	
	* Forward simulate variables for next cross section *
	replace Laction_rl = action
	replace Laction_index = action if action != 90
	replace Laction_index = Laction_rl if action == 90
	
	replace age_index = age_index+1
	foreach act in ///
	"11" "12" "13" ///
	"20" "30" ///
	{
		replace exper_`act'_index = exper_`act'_index + 1 if action == `act'
	}
	
	keep $keep_vars
	order $keep_vars
	local big_T = `big_T'+1
	}
	
	* Calculate variance of idiosyncratic shocks for payoff functions (to be used below to simulate earnings) *
	local datadir T:\${data_folder}\
	use "`datadir'gc_analyze_me_tmp_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", clear
	foreach var in "state" "age" ///
	"exper_11" "exper_12" "exper_13" ///
	"exper_20" "exper_30" ///
	{
		rename `var' `var'_index
	}
	merge m:1 pid_int state_index using "`datadir'gc_structural_Qs_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta"
	drop if _merge == 2
	drop _merge
	merge m:1 state_index age_index ///
	exper_11_index exper_12_index exper_13_index ///
	exper_20_index exper_30_index ///
	using "`datadir'gc_structural_ln_y_hats_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta"
	drop if _merge == 2
	drop _merge
	foreach var in "state" "age" ///
	"exper_11" "exper_12" "exper_13" ///
	"exper_20" "exper_30" ///
	{
		rename `var'_index `var'
	}
	
	gen ln_y_hat = .
	foreach act in ///
	"11" "12" "13" ///
	"20" "30" ///
	{
		replace ln_y_hat = ln_y_hat_`act' if action == `act'
	}
	sort pid_int tax_yr state
	gen mu = ln_y-ln_y_hat
	sort action
	gen sigma = .
	foreach act in ///
	"11" "12" "13" ///
	"20" "30" ///
	{
		sum mu [aw=q] if action == `act'
		replace sigma = `r(sd)' if action == `act'
	}
	keep action sigma
	duplicates drop
	save "`datadir'gc_structural_sigmas_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", replace

	use "`datadir'gc_tmpfitc5_1_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", clear
	forvalues t = 2(1)10 ///
	{
		append using "`datadir'gc_tmpfitc5_`t'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta"
	}
	merge m:1 action using "`datadir'gc_structural_sigmas_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta"
	drop if _merge == 2
	drop _merge
	gen shock = rnormal(0,sigma)
	gen ln_y = ln_y_hat+shock
	compress
	save "`datadir'gc_structural_C5_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", replace
	clear
	
	capture erase "`datadir'gc_structural_sigmas_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta"
	forvalues t = 1(1)10 ///
	{
		capture erase "`datadir'gc_tmpfitc5_`t'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta"
	}
	capture erase "`datadir'gc_TEMP_C5.dta"
		
	disp "***** Counterfactual 5 (COMPLETED) *****"
}
disp "***** Finished processing STEP 5 *****"
endtime

********************
* Counterfactual 6 *
********************
disp "***** Started processing STEP 6 *****"
starttime
if 1 == `gate6' ///
{
	disp "***** Counterfactual 6 *****"
	disp "***** Time is now $S_TIME *****"
	
	local datadir T:\${data_folder}\
	use "`datadir'gc_structural_fit_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", clear
	keep if age == 1
	keep pid_int state
	compress
	save "`datadir'gc_TEMP_C6.dta", replace
	use "`datadir'gc_analyze_me_tmp_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", clear
	keep if age == 1
	* keep a 20% random sample of individuals *
	gen draw = runiform()
	replace draw = . if state != 1
	bys pid_int: egen draw2 = max(draw)
	keep if draw2 <= 1
	drop draw*
	rename state state_index
	merge m:1 pid_int state_index using "`datadir'gc_structural_Qs_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta"
	drop if _merge == 2
	drop _merge
	rename state_index state
	reshape wide q, i(pid_int) j(state)
	keep pid_int q* age Laction exper_11 exper_12 exper_13 exper_20 exper_30 busiden_entry_cat
	order pid_int q* age Laction exper_11 exper_12 exper_13 exper_20 exper_30 busiden_entry_cat
	
	merge 1:1 pid_int using "`datadir'gc_TEMP_C6.dta"
	keep if _merge == 3
	drop _merge
	
	global keep_vars ""
	foreach var in ///
	"Laction" "age" /// 
	"exper_11" "exper_12" "exper_13" ///
	"exper_20" "exper_30" ///
	{
		gen `var'_index = `var'
		global keep_vars "$keep_vars `var'_index"
	}
	gen Laction_rl = Laction
	global keep_vars "pid_int state q* $keep_vars Laction_rl busiden_entry_cat"
	keep $keep_vars
	order $keep_vars
	
	local big_T = 1
	while `big_T' <= 10 ///
	{
	gen V_0 = 1
	
	***************************
	* STEP 1.2: FIX ACTION j' *
	***************************
	* Create synthetic variables *
	foreach var in ///
	"Laction" "age" /// 
	"exper_11" "exper_12" "exper_13" ///
	"exper_20" "exper_30" ///
	{
		gen `var' = `var'_index
	}
	
	* construct synthetic aggregate experience variables *
	gen exper_10 = exper_11 + exper_12 + exper_13	
	gen age_sq = age*age
	
	* construct polynomials of experience *
	foreach var in ///
	"exper_10" "exper_20" "exper_30" ///
	{
		gen `var'_sq = `var'*`var'
	}
	
	* Bring CCP matrix using appropriate STER files from first stage *
	
	* Create CCPs *
	foreach act in ///
	"11" "12" "13" ///
	"20" "30" "90" ///
	{
		gen CCP`act' = .
	}		
	
	local counter = 1
	
	* Loop through state *
	forvalues s = 1(1)`n_types' ///
	{
	* Loop through Laction *
	foreach Lact in ///
	"11" "12" "13" ///
	"20" "30" "90" ///
	{
		* Loop through action dummies *
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			disp "***** Now running linear probability model locally for: CCP action = `act', Laction = `Lact', and type = `s' *****"
			disp "***** Iteration `counter', time is now $S_TIME *****"
			local datadir T:\${data_folder}\gc_ster\
			estimates use "`datadir'gc_structural_CCP`act'_L`Lact'_s`s'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
			predict tmpCCP`act'_`Lact'_`s' if Laction == `Lact' & state == `s'
			estimates clear
			replace CCP`act' = tmpCCP`act'_`Lact'_`s' if Laction == `Lact' & state == `s'
			drop tmp*
			local counter = `counter' + 1
		}
	}
	}

	foreach act in ///
	"11" "12" "13" ///
	"20" "30" "90" ///
	{
		replace CCP`act' = 0.0000001 if CCP`act' <= 0.0000001
		replace CCP`act' = 0.9999999 if CCP`act' >= 0.9999999 & CCP`act' !=.
	}
	
	gen CCP_TOT = CCP11 + CCP12 + CCP13 + CCP20 + CCP30 + CCP90
	foreach act in ///
	"11" "12" "13" ///
	"20" "30" "90" ///
	{
		replace CCP`act' = CCP`act'/CCP_TOT
	}
	drop CCP_TOT
	
	* Pile up the CCPs into a sort of cdf *
	gen a = CCP11
	gen b = a + CCP12
	gen c = b + CCP13
	gen d = c + CCP20
	gen e = d + CCP30
	gen f = e + CCP90
	
	* Draw uniform (0,1) *
	gen draw = runiform()
	
	* Draw action *
	gen zaction_jp = 0
	replace zaction_jp = 11 if draw < a
	replace zaction_jp = 12 if draw >= a & draw < b
	replace zaction_jp = 13 if draw >= b & draw < c
	replace zaction_jp = 20 if draw >= c & draw < d
	replace zaction_jp = 30 if draw >= d & draw < e
	replace zaction_jp = 90 if draw >= e & draw <= 1
	
	drop CCP*
	
	* Drop all synthetic variables *
	keep $keep_vars z* V_*
	
	****************************
	* STEP 1.3: FIX ACTION j'' *
	****************************
	* Create synthetic variables *
	foreach var in ///
	"Laction" "age" /// 
	"exper_11" "exper_12" "exper_13" ///
	"exper_20" "exper_30" ///
	{
		gen `var' = `var'_index
	}
	
	* Forward simulate synthetic variables *
	replace Laction = zaction_jp
	replace age = age + 1

	foreach act in ///
	"11" "12" "13" ///
	"20" "30" ///
	{
		replace exper_`act' = exper_`act' + 1 if zaction_jp == `act'
	}
	
	* construct synthetic aggregate experience variables *
	gen exper_10 = exper_11 + exper_12 + exper_13	
	gen age_sq = age*age
	
	* construct polynomials of experience *
	foreach var in ///
	"exper_10" "exper_20" "exper_30" ///
	{
		gen `var'_sq = `var'*`var'
	}
	
	* Bring CCP matrix using appropriate STER files from first stage *
	
	* Create CCPs *
	foreach act in ///
	"11" "12" "13" ///
	"20" "30" "90" ///
	{
		gen CCP`act' = .
	}
	
	local counter = 1
	* Loop through state *
	forvalues s = 1(1)`n_types' ///
	{
	* Loop through Laction *
	foreach Lact in ///
	"11" "12" "13" ///
	"20" "30" "90" ///
	{
		* Loop through action dummies *
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			disp "***** Now running linear probability model locally for: CCP action = `act', Laction = `Lact', and type = `s' *****"
			disp "***** Iteration `counter', time is now $S_TIME *****"
			local datadir T:\${data_folder}\gc_ster\
			estimates use "`datadir'gc_structural_CCP`act'_L`Lact'_s`s'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
			predict tmpCCP`act'_`Lact'_`s' if Laction == `Lact' & state == `s'
			estimates clear
			replace CCP`act' = tmpCCP`act'_`Lact'_`s' if Laction == `Lact' & state == `s'
			drop tmp*
			local counter = `counter' + 1
		}
	}
	}
	
	foreach act in ///
	"11" "12" "13" ///
	"20" "30" "90" ///
	{
		replace CCP`act' = 0.0000001 if CCP`act' <= 0.0000001
		replace CCP`act' = 0.9999999 if CCP`act' >= 0.9999999 & CCP`act' !=.
	}
	
	gen CCP_TOT = CCP11 + CCP12 + CCP13 + CCP20 + CCP30 + CCP90
	foreach act in ///
	"11" "12" "13" ///
	"20" "30" "90" ///
	{
		replace CCP`act' = CCP`act'/CCP_TOT
	}
	drop CCP_TOT
	
	* Pile up the CCPs into a sort of cdf *
	gen a = CCP11
	gen b = a + CCP12
	gen c = b + CCP13
	gen d = c + CCP20
	gen e = d + CCP30
	gen f = e + CCP90
	
	* Draw uniform (0,1) *
	gen draw = runiform()
	
	* Draw action j'' *
	gen zaction_jpp = 0
	replace zaction_jpp = 11 if draw < a
	replace zaction_jpp = 12 if draw >= a & draw < b
	replace zaction_jpp = 13 if draw >= b & draw < c
	replace zaction_jpp = 20 if draw >= c & draw < d
	replace zaction_jpp = 30 if draw >= d & draw < e
	replace zaction_jpp = 90 if draw >= e & draw <= 1
	drop CCP*
	
	* Drop all synthetic variables *
	keep $keep_vars z* V_*
	
	***************************************************
	* STEP 1.4: FOR EACH j in J CALCULATE OFFSET TERM *
	***************************************************
	foreach j in ///
	"11" "12" "13" ///
	"20" "30" "90" ///
	{
		* Pick action j *
		gen zaction_j = `j'
		* Pick action cj *
		gen zaction_cj = zaction_jp
		* Pick action cjp *
		gen zaction_cjp = zaction_j
		* Pick action cjpp *
		gen zaction_cjpp = zaction_jpp
		
		****************************
		* CCPs and income associated with j *
		****************************
		* Create synthetic variables *
		foreach var in ///
		"Laction" "age" /// 
		"exper_11" "exper_12" "exper_13" ///
		"exper_20" "exper_30" ///
		{
			gen `var' = `var'_index
		}
		
		* construct synthetic aggregate experience variables *
		gen exper_10 = exper_11 + exper_12 + exper_13	
		gen age_sq = age*age
		
		* construct polynomials of experience *
		foreach var in ///
		"exper_10" "exper_20" "exper_30" ///
		{
			gen `var'_sq = `var'*`var'
		}
	
		* Bring ln_y_hat matrix using appropriate STER files from first stage *
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" ///
		{
			disp "***** Now estimating parameters of the wage equation for action: `act' *****"
			disp "***** Time is now $S_TIME *****"
			local datadir T:\${data_folder}\gc_ster\
			estimates use "`datadir'gc_structural_y`act'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
			predict ln_y_hat_`act'
			estimates clear
		}
		
		* Pick the relevant ln_y_hats *
		gen zln_y_hat_j = 0
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" ///
		{
			replace zln_y_hat_j = ln_y_hat_`act' if zaction_j == `act'
		}
		*** COUNTERFACTUAL CHANGE ***
		if `big_T' == 5 ///
		{
			disp "BIG T is 1. COUNTERFACTUAL ACTIVATED"
			replace zln_y_hat_j = zln_y_hat_j + ln(2) if zaction_j == 30
		}
		replace zln_y_hat_j = ln(10400) if zaction_j == 90
		drop ln_y_hat*
	
		* Drop all synthetic variables *
		keep $keep_vars z* V_*
		
		****************************
		* CCPs and income associated with j'. *
		****************************
		* Create synthetic variables *
		foreach var in ///
		"Laction" "age" /// 
		"exper_11" "exper_12" "exper_13" ///
		"exper_20" "exper_30" ///
		{
			gen `var' = `var'_index
		}
		
		* Forward simulate synthetic variables *
		replace Laction = zaction_j
		replace age = age + 1
	
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" ///
		{
			replace exper_`act' = exper_`act' + 1 if zaction_j == `act'
		}
		
		* construct synthetic aggregate experience variables *
		gen exper_10 = exper_11 + exper_12 + exper_13	
		gen age_sq = age*age
		
		* construct polynomials of experience *
		foreach var in ///
		"exper_10" "exper_20" "exper_30" ///
		{
			gen `var'_sq = `var'*`var'
		}
		
		* Bring CCP matrix using appropriate STER files from first stage *
		
		* Create CCPs *
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			gen CCP`act' = .
		}
		
		local counter = 1
		* Loop through state *
		forvalues s = 1(1)`n_types' ///
		{
		* Loop through Laction *
		foreach Lact in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			* Loop through action dummies *
			foreach act in ///
			"11" "12" "13" ///
			"20" "30" "90" ///
			{
				disp "***** Now running linear probability model locally for: CCP action = `act', Laction = `Lact', and type = `s' *****"
				disp "***** Iteration `counter', time is now $S_TIME *****"
				local datadir T:\${data_folder}\gc_ster\
				estimates use "`datadir'gc_structural_CCP`act'_L`Lact'_s`s'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
				predict tmpCCP`act'_`Lact'_`s' if Laction == `Lact' & state == `s'
				estimates clear
				replace CCP`act' = tmpCCP`act'_`Lact'_`s' if Laction == `Lact' & state == `s'
				drop tmp*
				local counter = `counter' + 1
			}
		}
		}
		
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			replace CCP`act' = 0.0000001 if CCP`act' <= 0.0000001
			replace CCP`act' = 0.9999999 if CCP`act' >= 0.9999999 & CCP`act' !=.
		}
		
		gen CCP_TOT = CCP11 + CCP12 + CCP13 + CCP20 + CCP30 + CCP90
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			replace CCP`act' = CCP`act'/CCP_TOT
		}
		
		drop CCP_TOT
		
		* Pick the relevant CCPs *
		gen zCCP_jp = .
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			replace zCCP_jp = CCP`act' if zaction_jp == `act'
		}
		
		drop CCP*
		
		* Bring ln_y_hat matrix using appropriate STER files from first stage *
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" ///
		{
			disp "***** Now estimating parameters of the wage equation for action: `act' *****"
			disp "***** Time is now $S_TIME *****"
			local datadir T:\${data_folder}\gc_ster\
			estimates use "`datadir'gc_structural_y`act'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
			predict ln_y_hat_`act'
			estimates clear
		}
		
		* Pick the relevant ln_y_hats *
		gen zln_y_hat_jp = 0
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" ///
		{
			replace zln_y_hat_jp = ln_y_hat_`act' if zaction_jp == `act'
		}
		replace zln_y_hat_jp = ln(10400) if zaction_jp == 90
		drop ln_y_hat*
		
		keep $keep_vars z* V_*
		
		****************************************
		* CCPs and income associated with j''. *
		****************************************
		* Create synthetic variables *
		foreach var in ///
		"Laction" "age" /// 
		"exper_11" "exper_12" "exper_13" ///
		"exper_20" "exper_30" ///
		{
			gen `var' = `var'_index
		}
		
		* Forward simulate synthetic variables *
		replace Laction = zaction_jp
		replace age = age + 2
	
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" ///
		{
			replace exper_`act' = exper_`act' + 1 if zaction_j == `act'
			replace exper_`act' = exper_`act' + 1 if zaction_jp == `act'
		}
		
		* construct synthetic aggregate experience variables *
		gen exper_10 = exper_11 + exper_12 + exper_13	
		gen age_sq = age*age
		
		* construct polynomials of experience *
		foreach var in ///
		"exper_10" "exper_20" "exper_30" ///
		{
			gen `var'_sq = `var'*`var'
		}
		
		* Bring CCP matrix using appropriate STER files from first stage *
		
		* Create CCPs *
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			gen CCP`act' = .
		}
		
		local counter = 1
		* Loop through state *
		forvalues s = 1(1)`n_types' ///
		{
		* Loop through Laction *
		foreach Lact in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			* Loop through action dummies *
			foreach act in ///
			"11" "12" "13" ///
			"20" "30" "90" ///
			{
				disp "***** Now running linear probability model locally for: CCP action = `act', Laction = `Lact', and type = `s' *****"
				disp "***** Iteration `counter', time is now $S_TIME *****"
				local datadir T:\${data_folder}\gc_ster\
				estimates use "`datadir'gc_structural_CCP`act'_L`Lact'_s`s'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
				predict tmpCCP`act'_`Lact'_`s' if Laction == `Lact' & state == `s'
				estimates clear
				replace CCP`act' = tmpCCP`act'_`Lact'_`s' if Laction == `Lact' & state == `s'
				drop tmp*
				local counter = `counter' + 1
			}
		}
		}
		
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			replace CCP`act' = 0.0000001 if CCP`act' <= 0.0000001
			replace CCP`act' = 0.9999999 if CCP`act' >= 0.9999999 & CCP`act' !=.
		}
		
		gen CCP_TOT = CCP11 + CCP12 + CCP13 + CCP20 + CCP30 + CCP90
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			replace CCP`act' = CCP`act'/CCP_TOT
		}
		drop CCP_TOT
		
		* Pick the relevant CCPs *
		gen zCCP_jpp = .
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			replace zCCP_jpp = CCP`act' if zaction_jpp == `act'
		}
	
		drop CCP*
		
		* Bring ln_y_hat matrix using appropriate STER files from first stage *
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" ///
		{
			disp "***** Now estimating parameters of the wage equation for action: `act' *****"
			disp "***** Time is now $S_TIME *****"
			local datadir T:\${data_folder}\gc_ster\
			estimates use "`datadir'gc_structural_y`act'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
			predict ln_y_hat_`act'
			estimates clear
		}
		
		* Pick the relevant ln_y_hats *
		gen zln_y_hat_jpp = 0
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" ///
		{
			replace zln_y_hat_jpp = ln_y_hat_`act' if zaction_jpp == `act'
		}
		replace zln_y_hat_jpp = ln(10400) if zaction_jpp == 90
		drop ln_y_hat*
		
		* Drop all synthetic variables *
		keep $keep_vars z* V_*
	
		***************************************
		* CCPs and income associated with cj. *
		***************************************
		* Create synthetic variables *
		foreach var in ///
		"Laction" "age" /// 
		"exper_11" "exper_12" "exper_13" ///
		"exper_20" "exper_30" ///
		{
			gen `var' = `var'_index
		}
		
		* construct synthetic aggregate experience variables *
		gen exper_10 = exper_11 + exper_12 + exper_13	
		gen age_sq = age*age
		
		* construct polynomials of experience *
		foreach var in ///
		"exper_10" "exper_20" "exper_30" ///
		{
			gen `var'_sq = `var'*`var'
		}
		
		* Bring ln_y_hat matrix using appropriate STER files from first stage *
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" ///
		{
			disp "***** Now estimating parameters of the wage equation for action: `act' *****"
			disp "***** Time is now $S_TIME *****"
			local datadir T:\${data_folder}\gc_ster\
			estimates use "`datadir'gc_structural_y`act'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
			predict ln_y_hat_`act'
			estimates clear
		}
		
		* Pick the relevant ln_y_hats *
		gen zln_y_hat_cj = 0
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" ///
		{
			replace zln_y_hat_cj = ln_y_hat_`act' if zaction_cj == `act'
		}
		*** COUNTERFACTUAL CHANGE ***
		if `big_T' == 5 ///
		{
			disp "BIG T is 1. COUNTERFACTUAL ACTIVATED"
			replace zln_y_hat_cj = zln_y_hat_cj + ln(2) if zaction_cj == 30
		}
		replace zln_y_hat_cj = ln(10400) if zaction_cj == 90
		drop ln_y_hat*
		
		* Drop all synthetic variables *
		keep $keep_vars z* V_*
		
		****************************************
		* CCPs and income associated with cj'. *
		****************************************
		* Create synthetic variables *
		foreach var in ///
		"Laction" "age" /// 
		"exper_11" "exper_12" "exper_13" ///
		"exper_20" "exper_30" ///
		{
			gen `var' = `var'_index
		}
		
		* Forward simulate synthetic variables *
		replace Laction = zaction_cj
		replace age = age + 1
		
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" ///
		{
			replace exper_`act' = exper_`act' + 1 if zaction_cj == `act'
		}
		
		* construct synthetic aggregate experience variables *
		gen exper_10 = exper_11 + exper_12 + exper_13	
		gen age_sq = age*age
		
		* construct polynomials of experience *
		foreach var in ///
		"exper_10" "exper_20" "exper_30" ///
		{
			gen `var'_sq = `var'*`var'
		}
		
		* Bring CCP matrix using appropriate STER files from first stage *
		
		* Create CCPs *
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			gen CCP`act' = .
		}		
		
		local counter = 1
		
		* Loop through state *
		forvalues s = 1(1)`n_types' ///
		{
		* Loop through Laction *
		foreach Lact in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			* Loop through action dummies *
			foreach act in ///
			"11" "12" "13" ///
			"20" "30" "90" ///
			{
				disp "***** Now running linear probability model locally for: CCP action = `act', Laction = `Lact', and type = `s' *****"
				disp "***** Iteration `counter', time is now $S_TIME *****"
				local datadir T:\${data_folder}\gc_ster\
				estimates use "`datadir'gc_structural_CCP`act'_L`Lact'_s`s'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
				predict tmpCCP`act'_`Lact'_`s' if Laction == `Lact' & state == `s'
				estimates clear
				replace CCP`act' = tmpCCP`act'_`Lact'_`s' if Laction == `Lact' & state == `s'
				drop tmp*
				local counter = `counter' + 1
			}
		}
		}
	
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			replace CCP`act' = 0.0000001 if CCP`act' <= 0.0000001
			replace CCP`act' = 0.9999999 if CCP`act' >= 0.9999999 & CCP`act' !=.
		}
		
		gen CCP_TOT = CCP11 + CCP12 + CCP13 + CCP20 + CCP30 + CCP90
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			replace CCP`act' = CCP`act'/CCP_TOT
		}
		drop CCP_TOT
		
		* Pick the relevant CCPs *
		gen zCCP_cjp = .
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			replace zCCP_cjp = CCP`act' if zaction_cjp == `act'
		}
		
		drop CCP*
		
		* Bring ln_y_hat matrix using appropriate STER files from first stage *
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" ///
		{
			disp "***** Now estimating parameters of the wage equation for action: `act' *****"
			disp "***** Time is now $S_TIME *****"
			local datadir T:\${data_folder}\gc_ster\
			estimates use "`datadir'gc_structural_y`act'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
			predict ln_y_hat_`act'
			estimates clear
		}
		
		* Pick the relevant ln_y_hats *
		gen zln_y_hat_cjp = 0
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" ///
		{
			replace zln_y_hat_cjp = ln_y_hat_`act' if zaction_cjp == `act'
		}
		replace zln_y_hat_cjp = ln(10400) if zaction_cjp == 90
		drop ln_y_hat*
		
		* Drop all synthetic variables *
		keep $keep_vars z* V_*
		
		******************************
		* CCPs associated with cj''. *
		******************************
		* Create synthetic variables *
		foreach var in ///
		"Laction" "age" /// 
		"exper_11" "exper_12" "exper_13" ///
		"exper_20" "exper_30" ///
		{
			gen `var' = `var'_index
		}
		
		* Forward simulate synthetic variables *
		replace Laction = zaction_cjp
		replace age = age + 2
	
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" ///
		{
			replace exper_`act' = exper_`act' + 1 if zaction_cj == `act'
			replace exper_`act' = exper_`act' + 1 if zaction_cjp == `act'
		}
		
		* construct synthetic aggregate experience variables *
		gen exper_10 = exper_11 + exper_12 + exper_13	
		gen age_sq = age*age
		
		* construct polynomials of experience *
		foreach var in ///
		"exper_10" "exper_20" "exper_30" ///
		{
			gen `var'_sq = `var'*`var'
		}
		
		* Bring CCP matrix using appropriate STER files from first stage *
		
		* Create CCPs *
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			gen CCP`act' = .
		}
		
		local counter = 1
		* Loop through state *
		forvalues s = 1(1)`n_types' ///
		{
		* Loop through Laction *
		foreach Lact in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			* Loop through action dummies *
			foreach act in ///
			"11" "12" "13" ///
			"20" "30" "90" ///
			{
				disp "***** Now running linear probability model locally for: CCP action = `act', Laction = `Lact', and type = `s' *****"
				disp "***** Iteration `counter', time is now $S_TIME *****"
				local datadir T:\${data_folder}\gc_ster\
				estimates use "`datadir'gc_structural_CCP`act'_L`Lact'_s`s'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
				predict tmpCCP`act'_`Lact'_`s' if Laction == `Lact' & state == `s'
				estimates clear
				replace CCP`act' = tmpCCP`act'_`Lact'_`s' if Laction == `Lact' & state == `s'
				drop tmp*
				local counter = `counter' + 1
			}
		}
		}
		
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			replace CCP`act' = 0.0000001 if CCP`act' <= 0.0000001
			replace CCP`act' = 0.9999999 if CCP`act' >= 0.9999999 & CCP`act' !=.
		}
		
		gen CCP_TOT = CCP11 + CCP12 + CCP13 + CCP20 + CCP30 + CCP90
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			replace CCP`act' = CCP`act'/CCP_TOT
		}
		drop CCP_TOT
		
		* Pick the relevant CCPs *
		gen zCCP_cjpp = .
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			replace zCCP_cjpp = CCP`act' if zaction_cjpp == `act'
		}
	
		drop CCP*
		
		* Bring ln_y_hat matrix using appropriate STER files from first stage *
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" ///
		{
			disp "***** Now estimating parameters of the wage equation for action: `act' *****"
			disp "***** Time is now $S_TIME *****"
			local datadir T:\${data_folder}\gc_ster\
			estimates use "`datadir'gc_structural_y`act'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
			predict ln_y_hat_`act'
			estimates clear
		}
		
		* Pick the relevant ln_y_hats *
		gen zln_y_hat_cjpp = 0
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" ///
		{
			replace zln_y_hat_cjpp = ln_y_hat_`act' if zaction_cjpp == `act'
		}
		replace zln_y_hat_cjpp = ln(10400) if zaction_cjpp == 90
		drop ln_y_hat*
		
		* Drop all synthetic variables *
		keep $keep_vars z* V_*
		
		local beta = .90
		display "`beta'"
		
		*************************
		* Calculate offset term *
		*************************
		gen Offset = `beta'*log(zCCP_jp/zCCP_cjp) + `beta'*`beta'*log(zCCP_jpp/zCCP_cjpp)
		
		********************************************************************
		* Calculate Delta U using estimated parameters of utility function *
		********************************************************************
		gen z_lny = zln_y_hat_j-zln_y_hat_cj + `beta'*(zln_y_hat_jp-zln_y_hat_cjp) + `beta'*`beta'*(zln_y_hat_jpp-zln_y_hat_cjpp)
	
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" ///
		{
			gen amen`act'_j = 0
			replace amen`act'_j = 1-`beta' if zaction_j == `act'
			gen amen`act'_jp = 0
			replace amen`act'_jp = -1*(1-`beta') if zaction_jp == `act'
			gen zamen`act' = amen`act'_j + amen`act'_jp
			drop amen*
		}
		
		display "`beta'"
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" ///
		{
			foreach Lact in ///
			"11" "12" "13" ///
			"20" "30" "90" ///
			{
				gen cost_act`Lact'_`act'_z_j = 0
				replace cost_act`Lact'_`act'_z_j = 1 if Laction_index == `Lact' & zaction_j == `act' & Laction_index != zaction_j
				gen cost_act`Lact'_`act'_z_jp = 0
				replace cost_act`Lact'_`act'_z_jp = -1 if Laction_index == `Lact' & zaction_jp == `act' & Laction_index != zaction_jp
				gen cost_act`Lact'_`act'_j_jp = 0
				replace cost_act`Lact'_`act'_j_jp = `beta' if zaction_j == `Lact' & zaction_jp == `act' & zaction_j != zaction_jp
				gen cost_act`Lact'_`act'_jp_j = 0
				replace cost_act`Lact'_`act'_jp_j = -`beta' if zaction_jp == `Lact' & zaction_j == `act' & zaction_jp != zaction_j
				gen cost_act`Lact'_`act'_jp_jpp = 0
				replace cost_act`Lact'_`act'_jp_jpp = `beta'*`beta' if zaction_jp == `Lact' & zaction_jpp == `act' & zaction_jp != zaction_jpp
				gen cost_act`Lact'_`act'_j_jpp = 0
				replace cost_act`Lact'_`act'_j_jpp = -`beta'*`beta' if zaction_j == `Lact' & zaction_jpp == `act' & zaction_j != zaction_jpp
				gen zcost_act`Lact'_`act' = cost_act`Lact'_`act'_z_j+cost_act`Lact'_`act'_z_jp+cost_act`Lact'_`act'_j_jp+cost_act`Lact'_`act'_jp_j+cost_act`Lact'_`act'_jp_jpp + cost_act`Lact'_`act'_j_jpp
				drop cost*
			}
		}

		disp "***** Time is now $S_TIME *****"
		local datadir T:\${data_folder}\gc_ster\
		estimates use "`datadir'gc_structural_util_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
		predict Delta_U
		estimates clear
	
		* Now calculate v_j *
		gen v_`j' = Delta_U - Offset
	
		* Now calculate e_j *
		gen rgumbel_`j' = -log(-log(runiform()))
			
		* Now calculate V_j *
		gen V_`j' = v_`j' + rgumbel_`j'

		keep $keep_vars zaction_jp zaction_jpp V_*
	}
		
	**********************************************************
	* Now choose action that maximizes V_j, call that action *
	**********************************************************
	gen maxV = max(V_11,V_12,V_13,V_20,V_30,V_90)
	gen action = 0
	foreach act in ///
	"11" "12" "13"  ///
	"20" "30" "90" ///
	{
		replace action = `act' if round(V_`act',.001) == round(maxV,.001)
	}

	* save cross-section before updating state variables *
	preserve
	foreach var in ///
	"Laction" "age" /// 
	"exper_11" "exper_12" "exper_13" ///
	"exper_20" "exper_30" ///
	{
		gen `var' = `var'_index
	}
	keep pid_int state q* age action Laction exper_11 exper_12 exper_13 exper_20 exper_30
	order pid_int state q* age action Laction exper_11 exper_12 exper_13 exper_20 exper_30
	gen t = `big_T'
	* construct aggregate experience variables *
	gen exper_10 = exper_11 + exper_12 + exper_13	
	gen age_sq = age*age
	
	* construct polynomials of experience *
	foreach var in ///
	"exper_10" "exper_20" "exper_30" ///
	{
		gen `var'_sq = `var'*`var'
	}
	
	* Simulate log earnings using estimated payoff functions *
	* Bring ln_y_hat matrix using appropriate STER files from first stage *
	foreach act in ///
	"11" "12" "13" ///
	"20" "30" ///
	{
		disp "***** Now estimating parameters of the wage equation for action: `act' *****"
		disp "***** Time is now $S_TIME *****"
		local datadir T:\${data_folder}\gc_ster\
		estimates use "`datadir'gc_structural_y`act'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
		predict ln_y_hat_`act'
		estimates clear
	}
		
	* Pick the relevant ln_y_hat *
	gen ln_y_hat = .
	foreach act in ///
	"11" "12" "13" ///
	"20" "30" ///
	{
		replace ln_y_hat = ln_y_hat_`act' if action == `act'
	}
	drop ln_y_hat_*
	
	local datadir T:\${data_folder}\
	disp "`datadir'"
	compress
	save "`datadir'gc_tmpfitc6_`big_T'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", replace
	restore
	
	* Forward simulate variables for next cross section *
	replace Laction_rl = action
	replace Laction_index = action if action != 90
	replace Laction_index = Laction_rl if action == 90
	
	replace age_index = age_index+1
	foreach act in ///
	"11" "12" "13" ///
	"20" "30" ///
	{
		replace exper_`act'_index = exper_`act'_index + 1 if action == `act'
	}
	
	keep $keep_vars
	order $keep_vars
	local big_T = `big_T'+1
	}
	
	* Calculate variance of idiosyncratic shocks for payoff functions (to be used below to simulate earnings) *
	local datadir T:\${data_folder}\
	use "`datadir'gc_analyze_me_tmp_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", clear
	foreach var in "state" "age" ///
	"exper_11" "exper_12" "exper_13" ///
	"exper_20" "exper_30" ///
	{
		rename `var' `var'_index
	}
	merge m:1 pid_int state_index using "`datadir'gc_structural_Qs_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta"
	drop if _merge == 2
	drop _merge
	merge m:1 state_index age_index ///
	exper_11_index exper_12_index exper_13_index ///
	exper_20_index exper_30_index ///
	using "`datadir'gc_structural_ln_y_hats_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta"
	drop if _merge == 2
	drop _merge
	foreach var in "state" "age" ///
	"exper_11" "exper_12" "exper_13" ///
	"exper_20" "exper_30" ///
	{
		rename `var'_index `var'
	}
	
	gen ln_y_hat = .
	foreach act in ///
	"11" "12" "13" ///
	"20" "30" ///
	{
		replace ln_y_hat = ln_y_hat_`act' if action == `act'
	}
	sort pid_int tax_yr state
	gen mu = ln_y-ln_y_hat
	sort action
	gen sigma = .
	foreach act in ///
	"11" "12" "13" ///
	"20" "30" ///
	{
		sum mu [aw=q] if action == `act'
		replace sigma = `r(sd)' if action == `act'
	}
	keep action sigma
	duplicates drop
	save "`datadir'gc_structural_sigmas_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", replace

	use "`datadir'gc_tmpfitc6_1_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", clear
	forvalues t = 2(1)10 ///
	{
		append using "`datadir'gc_tmpfitc6_`t'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta"
	}
	merge m:1 action using "`datadir'gc_structural_sigmas_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta"
	drop if _merge == 2
	drop _merge
	gen shock = rnormal(0,sigma)
	gen ln_y = ln_y_hat+shock
	compress
	save "`datadir'gc_structural_C6_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", replace
	clear
	
	capture erase "`datadir'gc_structural_sigmas_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta"
	forvalues t = 1(1)10 ///
	{
		capture erase "`datadir'gc_tmpfitc6_`t'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta"
	}
	capture erase "`datadir'gc_TEMP_C6.dta"
	
	disp "***** Counterfactual 6 (COMPLETED) *****"
}
disp "***** Finished processing STEP 6 *****"
endtime

}

********************************************
* STEP X: delete all intermediate datasets *
********************************************
disp "***** Started processing STEP X *****"
starttime
if 1 == `gateX' ///
{
	disp "***** STEP X: delete all intermediate datasets *****"
	local datadir T:\${data_folder}\
	
	disp "***** STEP X: delete all intermediate datasets (COMPLETED) *****"
}
disp "***** Finished processing STEP X *****"
endtime

********
* EXIT *
********
clear all
log close
